package com.h2psoft.truyen.tranh.activities;

import java.util.ArrayList;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.h2psoft.truyen.tranh.R;
import com.h2psoft.truyen.tranh.adapter.AdapterInComicWebView;
import com.h2psoft.truyen.tranh.constants.Constants;
import com.h2psoft.truyen.tranh.objects.ObjInComic;

public class FragmentInComic extends Fragment {
	private String tag = FragmentInComic.class.getCanonicalName();

	private AdapterInComicWebView adapterInComic;
	private String[] listUrlImages;
	private ArrayList<ObjInComic> arrInComics;
	private ListView lvInComic;

	public static Fragment newInstance(String[] listUrls) {
		FragmentInComic mFragment = new FragmentInComic();

		Bundle args = new Bundle();
		args.putStringArray(Constants.KEY_LIST_URL, listUrls);
		mFragment.setArguments(args);
		return mFragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_in_comic, container, false);
		init(v);
		return v;
	}

	private void init(View v) {
		lvInComic = (ListView) v.findViewById(R.id.lvlInComic);
		listUrlImages = getArguments().getStringArray(Constants.KEY_LIST_URL);
		arrInComics = new ArrayList<ObjInComic>();
		adapterInComic = new AdapterInComicWebView(getActivity(), arrInComics);
		lvInComic.setAdapter(adapterInComic);
		loadListContent();
	}

	private void loadListContent() {
		int listSize = listUrlImages.length;
		for (int i = 0; i < listSize; i++) {
			if (i % 2 == 0) {
				ObjInComic objInComic = new ObjInComic();
				objInComic.setImgUrl(listUrlImages[i]);
				objInComic.setSecondImgUrl(listUrlImages[i + 1]);
				arrInComics.add(objInComic);
			}
		}
		adapterInComic.notifyDataSetChanged();
		lvInComic.requestLayout();

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
	}

	/* Class that prevents opening the Browser */

}
